package com.yanqu.road.server.cmd.activity.washe.cross;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.data.WasheUserJinBiaoData;
import com.yanqu.road.entity.enums.washe.eWasheModeType;
import com.yanqu.road.logic.bussiness.activity.WasheBusiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.washe.WasheModule;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = GameProtocol.S_WASHE_MATCH_RESULT_FROM_CROSS, desc = "")
public class WasheMatchResultFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        long userId = yanQuMessage.getPlayerId();
        WasheProto.WasheMatchResultRespMsg respMsg = WasheProto.WasheMatchResultRespMsg.parseFrom(yanQuMessage.getBytes());
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
        // 开始游戏
        if (respMsg.getRet() == 0) {
            int modeId = respMsg.getMode();
            // 积分赛匹配成功扣除体力
            if (modeId == eWasheModeType.JinBiaoScoreLow.getValue() || modeId == eWasheModeType.JinBiaoScoreHigh.getValue()) {
                WasheConfig washeConfig = WasheMgr.getWasheConfig();
                // 获取体力消耗
                int costEnergy = modeId == eWasheModeType.JinBiaoScoreLow.getValue() ? washeConfig.getJinBiaoScoreLowCost() : washeConfig.getJinBiaoScoreHighCost();
                if (onlinePlayer != null) {
                    WasheUserJinBiaoData jinBiaoData = onlinePlayer.getModule(WasheModule.class).getWasheUserJinBiaoData();
                    jinBiaoData.setEnergy(jinBiaoData.getEnergy() - costEnergy);
                } else {
                    WasheUserJinBiaoData jinBiaoData = WasheBusiness.getWasheUserJinBiaoData(userId);
                    jinBiaoData.setEnergy(jinBiaoData.getEnergy() - costEnergy);
                    WasheBusiness.updateWasheUserJinBiaoData(jinBiaoData);
                }
            }
            if (onlinePlayer != null) {
                onlinePlayer.sendPacket(ClientProtocol.U_WASHE_MATCH_RESULT, respMsg.toBuilder());
            }
        }
    }
}
